var kugouPlayer = requirePlugin("kugouPlayer")
kugouPlayer.player.initPlayer({
  noClimax: false, //默认是播放歌曲的高潮片段，传true则播放前60秒
  playMode: 'cycle', //cycle列表循环（默认）  loop 顺序播放  once单曲播放  random随机播放
  positionTop: '80rpx', //挂件距离顶部的距离，默认80rpx
  positionRight: '24rpx', //挂件距离侧边的距离，默认24rpx
});

App({

  /**
   * 全局变量
   */

  globalData: {
    user_id: null,
  },
  api_root: 'https://aidc.jkjmei.com/api/', // api地址
  api_url:'https://aidc.jkjmei.com',
  location_x: 0,
  location_y: 0,

  onLaunch: function(options) {
    // console.log("首先进入app.js");
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        var windowHeight = (res.windowHeight * (750 / res.windowWidth));
        that.globalData.windowHeight = windowHeight;
        that.globalData.screenHeight = res.screenHeight;
        that.globalData.kuangaobi = 750 / res.windowWidth;
        that.globalData.statusBarHeight = res.statusBarHeight
        
        let modelmes = res.model;

        if(modelmes.search('iPhone X') == 0 || modelmes.search('iPhone12,1') == 0 || modelmes.search('iPhone XS') == 0) {
          that.globalData.isIphoneX = true;
        }else{
          that.globalData.isIphoneX = false; 
        }
      }
    })
    // 判断是否由分享进入小程序
    if (options.scene == 1007 || options.scene == 1008) {
      this.globalData.share = true
    } else {
      this.globalData.share = false
    }
    that.globalData.phonetel = /^(((13[0-9]{1})|(12[0-9]{1})|(14[0-9]{1})|(19[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    that.globalData.saveFriendList = [];

    wx.getStorageInfo({
      success (res) {
        // console.error(res.keys, res.currentSize)
        if(res.currentSize > (res.limitSize*0.8)) {
          for(var i = 0; i<res.keys.length; i++) {
            if(res.keys != "type" || res.keys != "user_id" || res.keys != "user_info"
                || res.keys != "student_id" || res.keys != "grades_id")
            {
              wx.removeStorage({key: res.keys, success: function (e) {}});
            }
          }
        }
      }
    })
  },
  player: kugouPlayer.player, //这样设置之后，在各页面就能通过getApp().player拿到全局播放器对象
  acc5ShowLoading: function(e) {
    e.setData({
      loading: !0
    });
  },
  acc5HideLoading: function(e) {
    e.setData({
      loading: !1
    });
  },
  visit: function(o, t, n, a) {
    var mt = t;
    wx.request({
      url: "https://aidc.jkjmei.com/api/" + o,
      data: mt,
      method: n || "POST",
      dataType: "json",
      header: {
        Authorization: wx.getStorageSync("user_id") ? wx.getStorageSync("user_id") : 0,
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(o) {
        mt.user_id = wx.getStorageSync("user_id");
        console.log('访问wjw接口发送的数据包 = ', mt);
        var t = o.data;
        a(t);
      },
      fail: function(e) {
        console.log(e);
      }
    });
  },

  /**
   * get请求
   */
  _get(url, data, success, fail, complete, check_login) {
    let App = this;
    wx.showNavigationBarLoading();

    // 构造请求参数
    data = Object.assign({
      wxapp_id: 10001,
      token: wx.getStorageSync('token')
    }, data);

    // if (typeof check_login === 'undefined')
    //   check_login = true;

    // 构造get请求
    let request = () => {
      data.token = wx.getStorageSync('token');
      wx.request({
        url: App.api_root + url,
        header: {
          'content-type': 'application/json'
        },
        data,
        success(res) {
          if (res.statusCode !== 200 || typeof res.data !== 'object') {
            console.log(res);
            App.showError('网络请求出错');
            return false;
          }
          if (res.data.code === -1) {
            // 登录态失效, 重新登录
            wx.hideNavigationBarLoading();
            App.doLogin();
          } else if (res.data.code === 0) {
            App.showError(res.data.msg);
            return false;
          } else {
            success && success(res.data);
          }
        },
        fail(res) {
          // console.log(res);
          App.showError(res.errMsg, () => {
            fail && fail(res);
          });
        },
        complete(res) {
          wx.hideNavigationBarLoading();
          complete && complete(res);
        },
      });
    };
    // 判断是否需要验证登录
    check_login ? App.doLogin(request) : request();
  },


  /**
   * post提交
   */
  _post_form(url, data, success, fail, complete) {
    wx.showNavigationBarLoading();
    let App = this;
    // 构造请求参数
    data = Object.assign({
      wxapp_id: 10001,
      token: wx.getStorageSync('token')
    }, data);
    wx.request({
      url: App.api_root + url,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: 'POST',
      data,
      success(res) {
        if (res.statusCode !== 200 || typeof res.data !== 'object') {
          App.showError('网络请求出错');
          return false;
        }
        if (res.data.code === -1) {
          // 登录态失效, 重新登录
          App.doLogin(() => {
            App._post_form(url, data, success, fail);
          });
          return false;
        }
        success && success(res.data);
      },
      fail(res) {
        App.showError(res.errMsg, () => {
          fail && fail(res);
        });
      },
      complete(res) {
        wx.hideLoading();
        wx.hideNavigationBarLoading();
        complete && complete(res);
      }
    });
  },

  myToast: function (msg) {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 700,
      mask: true
    })
  },

  showToast: function(msg, icon, timer) {
    if(!icon) {
      icon = 'none';
    }
    if(!timer) { timer = 2000; }
    wx.showToast({
      title: msg,
      icon: icon,
      duration: timer
    })
  },
  stuRz: function() {
    if(wx.getStorageSync("type")==2) {
      if(!wx.getStorageSync("student_id") || wx.getStorageSync("student_id") == 'undefined') {
        wx.showModal({
          title: '提示',
          content: '您还没有认证学生，请先认证',
          success: function(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: "/pages/my/stuRz"
              });
            } else if (res.cancel) {
              wx.switchTab({
                url: "/pages/index/index"
              });
            }
          }
        })
      }
    }
  },
  removeArr: function (array,index) {
    if(index<=(array.length-1)){
      for(var i=index;i<array.length;i++){
        array[i]=array[i+1];
      }
    }
    else{
      throw new Error('超出最大索引！');
    }
    array.length=array.length-1;
    return array;
  },
  /**
   * 显示失败提示框
   */
  showError(msg, callback) {
    wx.showModal({
      title: '友情提示',
      content: msg,
      showCancel: false,
      success(res) {
        // callback && (setTimeout(() => {
        //   callback();
        // }, 1500));
        callback && callback();
      }
    });
  },
  /**
   * 显示成功提示框
   */
  showSuccess(msg, callback) {
    wx.showToast({
      title: msg,
      icon: 'success',
      success() {
        callback && (setTimeout(() => {
          callback();
        }, 2500));
      }
    });
  },

  urlEncode(data) {
    var _result = [];
    for (var key in data) {
      var value = data[key];
      _result.push(key + '=' + value);
    }
    return _result.join('&');
  },
  api_url: "https://aidc.jkjmei.com",
  globalData: {
    userInfo: null,
    isIphoneX: false,
    city:null
  },
  pushRefresh: {
    xlRef: "正在重新加载",
    slMore: "正在加载更多"
  },
  kuangaobi: 2,
  windowHeight: 1000,
  statusBarHeight: 0,
  share: false,
  saveFriendList: [],
  phonetel: "",
  sysInfo: function () {
    let res = wx.getSystemInfoSync();
    let info = {
      width: res.windowWidth,//可使用窗口宽度
      height: res.windowHeight,//可使用窗口高度
      system: res.system,//操作系统版本
      statusBarHeight: res.statusBarHeight//状态栏的高度
    }
    return info;
  }
});

